/*
 * Copyright (C) 2010 Wim Crols <twimpiex@gnx.be>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 3
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package be.gnx.event.consumer.receiver.model;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.Vector;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;

public class LogbackEvent implements Event {

  private static final long serialVersionUID = 45782356487349l;

  private ILoggingEvent loggingEvent;

  public LogbackEvent(ILoggingEvent loggingEvent) {
    this.loggingEvent = loggingEvent;
  }

  public Class getEventClass() {
    return ILoggingEvent.class;
  }

  public Object getEventObject() {
    return loggingEvent;
  }

  public EventLevel getEventLevel() {
    if (loggingEvent.getLevel().toInt() ==  Level.ALL.toInt()) {
      return EventLevel.ALL;
    }
    if (loggingEvent.getLevel().toInt() ==  Level.TRACE.toInt()) {
      return EventLevel.TRACE;
    }
    if (loggingEvent.getLevel().toInt() ==  Level.DEBUG.toInt()) {
      return EventLevel.DEBUG;
    }
    if (loggingEvent.getLevel().toInt() ==  Level.INFO.toInt()) {
      return EventLevel.INFO;
    }
    if (loggingEvent.getLevel().toInt() ==  Level.WARN.toInt()) {
      return EventLevel.WARN;
    }
    if (loggingEvent.getLevel().toInt() ==  Level.ERROR.toInt()) {
      return EventLevel.ERROR;
    }
    if (loggingEvent.getLevel().toInt() ==  Level.OFF.toInt()) {
      return EventLevel.OFF;
    }
    return EventLevel.ALL;
  }

  public Date getTimeStamp() {
    return new Date(loggingEvent.getTimeStamp());
  }

  public Object getMessage() {
    return loggingEvent.getMessage();
  }

  public Location getLocation() {
    return new Location() {
      @Override
      public String getFullyQualifiedClassName() {
    	  // TODO
        return loggingEvent.toString();
      }

      @Override
      public String getMethodName() {
    	  // TODO
          return loggingEvent.toString();
      }

      @Override
      public String getFileName() {
    	  // TODO
          return loggingEvent.toString();
      }

      @Override
      public Integer getLineNumber() {
    	  // TODO
          return 0;
      }
    };
  }

  public StackTrace getStackTrace() {
    return new StackTrace() {
      @Override
      public Vector<Location> getLocations() {
        Vector<Location> locations = new Vector<Location>();
        for (final StackTraceElement stackTraceElement : loggingEvent.getCallerData()) {
          Location location = new Location() {
            @Override
            public String getFullyQualifiedClassName() {
              return stackTraceElement.getClassName();
            }

            @Override
            public String getMethodName() {
                return stackTraceElement.getMethodName();
            }

            @Override
            public String getFileName() {
                return stackTraceElement.getFileName();
            }

            @Override
            public Integer getLineNumber() {
                return stackTraceElement.getLineNumber();
            }
          };
          locations.add(location);
        }
        return locations;
      }
    };
  }

  public String getThreadName() {
    return loggingEvent.getThreadName();
  }

  public String getLoggerName() {
    return loggingEvent.getLoggerName();
  }

  public String getNDC() {
	  // TODO
    return loggingEvent.toString();
  }

  public void serialize(OutputStream outputStream) {
    try {
      ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
      objectOutputStream.writeObject(loggingEvent);
    } catch (IOException ioException) {
      throw new RuntimeException(ioException);
    }
  }

}
